$(document).ready(function() {
    // 导航栏激活状态切换
    $('.catcafe_nav_link').on('click', function(e) {
        $('.catcafe_nav_link').removeClass('catcafe_active');
        $(this).addClass('catcafe_active');
    });

    // 猫咪轮播图功能
    let catsSlidePosition = 0;
    const $catsContainer = $('#catcafe_cats_slider');
    const $catsCards = $('.catcafe_cats_card');
    const catsCardWidth = $catsCards.outerWidth(true);
    const catsVisibleCards = Math.floor($catsContainer.width() / catsCardWidth);
    const catsMaxPosition = $catsCards.length - catsVisibleCards;
    
    // 隐藏超出可见区域的猫咪卡片
    $catsContainer.css('position', 'relative');
    
    // 轮播切换函数
    function slideCats(direction) {
        if (direction === 'prev') {
            catsSlidePosition = Math.max(0, catsSlidePosition - 1);
        } else if (direction === 'next') {
            catsSlidePosition = Math.min(catsMaxPosition, catsSlidePosition + 1);
        }
        
        $catsContainer.find('.catcafe_cats_card').css({
            'transform': `translateX(${-catsCardWidth * catsSlidePosition}px)`,
            'transition': 'transform 0.5s ease-in-out'
        });
        
        // 更新按钮状态
        updateCatsSliderButtons();
    }
    
    // 更新按钮状态
    function updateCatsSliderButtons() {
        if (catsSlidePosition <= 0) {
            $('#catcafe_cats_prev').prop('disabled', true).css('opacity', 0.5);
        } else {
            $('#catcafe_cats_prev').prop('disabled', false).css('opacity', 1);
        }
        
        if (catsSlidePosition >= catsMaxPosition) {
            $('#catcafe_cats_next').prop('disabled', true).css('opacity', 0.5);
        } else {
            $('#catcafe_cats_next').prop('disabled', false).css('opacity', 1);
        }
    }
    
    // 初始化按钮状态
    updateCatsSliderButtons();
    
    // 绑定按钮事件
    $('#catcafe_cats_prev').on('click', function() {
        slideCats('prev');
    });
    
    $('#catcafe_cats_next').on('click', function() {
        slideCats('next');
    });
    
    // 自动轮播
    let catsAutoSlide = setInterval(function() {
        if (catsSlidePosition < catsMaxPosition) {
            slideCats('next');
        } else {
            catsSlidePosition = -1;
            slideCats('next');
        }
    }, 4000);
    
    // 鼠标悬停时暂停轮播
    $catsContainer.on('mouseenter', function() {
        clearInterval(catsAutoSlide);
    }).on('mouseleave', function() {
        catsAutoSlide = setInterval(function() {
            if (catsSlidePosition < catsMaxPosition) {
                slideCats('next');
            } else {
                catsSlidePosition = -1;
                slideCats('next');
            }
        }, 4000);
    });

    // 通告栏滚动效果
    function scrollNotice() {
        const $noticeContent = $('#catcafe_notice_content p');
        const noticeWidth = $noticeContent.width();
        
        $noticeContent.css({
            'transform': 'translateX(100%)',
            'transition': 'none'
        });
        
        setTimeout(function() {
            $noticeContent.css({
                'transform': 'translateX(-100%)',
                'transition': 'transform 15s linear'
            });
            
            setTimeout(scrollNotice, 15000);
        }, 100);
    }
    
    scrollNotice();

    // 表单验证
    $('#catcafe_reservation_form').on('submit', function(e) {
        e.preventDefault();
        
        let isValid = true;
        const $form = $(this);
        const $name = $('#catcafe_name');
        const $phone = $('#catcafe_phone');
        const $date = $('#catcafe_date');
        const $time = $('#catcafe_time');
        const $persons = $('#catcafe_persons');
        
        // 简单验证
        if ($name.val().trim() === '') {
            showError($name, '请输入您的姓名');
            isValid = false;
        } else {
            removeError($name);
        }
        
        if ($phone.val().trim() === '') {
            showError($phone, '请输入您的电话号码');
            isValid = false;
        } else if (!/^1[3-9]\d{9}$/.test($phone.val().trim())) {
            showError($phone, '请输入有效的手机号码');
            isValid = false;
        } else {
            removeError($phone);
        }
        
        if ($date.val() === '') {
            showError($date, '请选择日期');
            isValid = false;
        } else {
            const selectedDate = new Date($date.val());
            const today = new Date();
            today.setHours(0, 0, 0, 0);
            
            if (selectedDate < today) {
                showError($date, '请选择今天或之后的日期');
                isValid = false;
            } else {
                removeError($date);
            }
        }
        
        if ($time.val() === '') {
            showError($time, '请选择时间段');
            isValid = false;
        } else {
            removeError($time);
        }
        
        if ($persons.val() === '' || $persons.val() < 1) {
            showError($persons, '请输入有效的人数');
            isValid = false;
        } else {
            removeError($persons);
        }
        
        if (isValid) {
            // 表单提交成功
            $form.find('.catcafe_form_submit').prop('disabled', true).text('提交中...');
            
            // 模拟表单提交
            setTimeout(function() {
                $form.html('<div class="catcafe_form_success"><i class="fas fa-check-circle"></i><p>预约成功！我们会尽快与您联系确认详情。</p></div>');
            }, 1500);
        }
    });
    
    // 显示错误信息
    function showError($input, message) {
        const $parent = $input.parent();
        $parent.addClass('catcafe_form_error');
        
        if ($parent.find('.catcafe_error_message').length === 0) {
            $parent.append(`<div class="catcafe_error_message">${message}</div>`);
        } else {
            $parent.find('.catcafe_error_message').text(message);
        }
    }
    
    // 移除错误信息
    function removeError($input) {
        const $parent = $input.parent();
        $parent.removeClass('catcafe_form_error');
        $parent.find('.catcafe_error_message').remove();
    }

    // 平滑滚动
    $('a[href^="#"]').on('click', function(e) {
        e.preventDefault();
        
        const target = $(this).attr('href');
        
        if (target === '#') return;
        
        $('html, body').animate({
            scrollTop: $(target).offset().top - 80
        }, 800);
    });

    // 响应式导航栏
    $(window).on('resize', function() {
        // 重新计算猫咪轮播图参数
        const newCatsCardWidth = $catsCards.outerWidth(true);
        const newCatsVisibleCards = Math.floor($catsContainer.width() / newCatsCardWidth);
        
        if (newCatsCardWidth !== catsCardWidth) {
            catsSlidePosition = 0;
            $catsContainer.find('.catcafe_cats_card').css({
                'transform': 'translateX(0)',
                'transition': 'none'
            });
            
            updateCatsSliderButtons();
        }
    });
}); 